import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/models/car_list_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/net/NetUtil.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/routes/routes.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';
import 'package:yurun_smartmerchants/view/EmptyView.dart';
import 'package:yurun_smartmerchants/view/custom_loadmore_footer.dart';

//已进场列表
class ApproachList extends StatefulWidget {
  int tidyStatus; // 0:待进场,1:已进场

  ApproachList(this.tidyStatus) {}

  @override
  _ApproachListState createState() => _ApproachListState();
}

class _ApproachListState extends State<ApproachList> {
  int page = 0;
  final int size = 10;
  List<CarListContent> carList;
  EasyRefreshController controller = EasyRefreshController();
  bool isIntoPageRefresh = true; //判断是不是第一次进入页面的刷新

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      getList();
    });
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  void getList() {
    if (isIntoPageRefresh) {
      LoadingUtil.showLoading(context, tip: '加载中...');
    }
    ApiInterface.getCarList(size, page, widget.tidyStatus).then((value) {
      CarListEntity carListEntity = CarListEntity.fromJson(value);
      setState(() {
        if (page == 0) {
          carList = carListEntity.content;
        } else {
          carList.addAll(carListEntity.content);
        }
      });
      overLoad(true, carListEntity.isLast);
      if (carList != null && carList.length > 0) {
        if (!isIntoPageRefresh && page == 0) {
          showToast("刷新成功");
        }
      }
    }).catchError((error) {
      overLoad(false, true);
    }).whenComplete(() => {
          if (isIntoPageRefresh) {LoadingUtil.hideLoading(context)}
        });
  }

  void overLoad(bool isSuccess, bool islast) {
    if (page == 0) {
      controller.finishRefresh(success: isSuccess);
      controller.finishLoad(success: isSuccess, noMore: islast);
    } else {
      controller.finishLoad(success: isSuccess, noMore: islast);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: double.infinity,
        height: double.infinity,
        color: Colours.gray_ef,
        child: Stack(
          children: <Widget>[
            Container(
                margin: EdgeInsets.only(top: AppSize.sp(20)),
                child: EasyRefresh(
                  controller: controller,
                  header: MaterialHeader(),
                  footer: CustomLoadFooter(),
                  onRefresh: () async {
                    isIntoPageRefresh = false;
                    page = 0;
                    getList();
                  },
                  onLoad: () async {
                    isIntoPageRefresh = false;
                    page++;
                    getList();
                  },
                  child: ListView.builder(
                      itemBuilder: (context, index) {
                        return getItem(context, index);
                      },
                      itemCount: carList == null ? 0 : carList.length),
                )),
          //  EmptyView.show(carList != null && carList.length == 0 ? false : true, null, widget.tidyStatus==0?"暂无待进场列表":"暂无已进场列表"),
            EmptyView(
              isHide:carList != null && carList.length == 0 ? false : true,
              content:widget.tidyStatus==0?"暂无待进场列表":"暂无已进场列表" ,
            )
          ],
        ));
  }

  Widget getItem(BuildContext context, int index) {
    return GestureDetector(
      onTap: (){
        Navigator.pushNamed(context, approach_detail);
      },
      child: Container(
        margin: EdgeInsets.only(bottom: AppSize.sp(20)),
        child: Column(
          children: <Widget>[
            Container(
              color: Colours.color_f8f8f8,
              width: double.infinity,
              height: AppSize.height(76),
              alignment: Alignment.center,
              child: Row(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(
                        left: AppSize.sp(32), right: AppSize.sp(16)),
                    width: AppSize.width(5),
                    height: AppSize.height(28),
                    color: Colours.color_dd4b39,
                  ),
                  Text(
                    "进场号: " + carList[index].no,
                    style: TextStyle(
                        color: Colours.color_313131, fontSize: AppSize.sp(32)),
                  ),
                  Spacer(),
                  Container(
                    margin: EdgeInsets.only(right: AppSize.sp(32)),
                    child: Text(
                      carList[index].createdTime,
                      style: TextStyle(
                          fontSize: AppSize.sp(26), color: Colours.color_898989),
                    ),
                  )
                ],
              ),
            ),
            Container(
              width: double.infinity,
              height: AppSize.height(178),
              color: Colours.white,
              alignment: Alignment.center,
              child: Column(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(
                        left: AppSize.sp(32), top: AppSize.sp(34)),
                    child: Row(
                      children: <Widget>[
                        Image.asset(
                          'images/approcah_shop.png',
                          width: AppSize.width(46),
                          height: AppSize.height(46),
                        ),
                        Container(
                          margin: EdgeInsets.only(
                              left: AppSize.sp(26), right: AppSize.sp(26)),
                          child: Text(
                            "商品:",
                            style: TextStyle(
                                fontSize: AppSize.sp(30),
                                color: Colours.color_898989),
                          ),
                        ),
                        Text(
                          carList[index].categoryNames,
                          style: TextStyle(
                              fontSize: AppSize.sp(30),
                              color: Colours.color_5b5b5b),
                        )
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(
                        left: AppSize.sp(32), top: AppSize.sp(22)),
                    child: Row(
                      children: <Widget>[
                        Image.asset(
                          'images/approach_stall.png',
                          width: AppSize.width(46),
                          height: AppSize.height(46),
                        ),
                        Container(
                          margin: EdgeInsets.only(
                              left: AppSize.sp(26), right: AppSize.sp(26)),
                          child: Text(
                            "车辆:",
                            style: TextStyle(
                                fontSize: AppSize.sp(30),
                                color: Colours.color_898989),
                          ),
                        ),
                        Text(
                          carList[index].vehicleType +
                              " " +
                              carList[index].vehicleNo,
                          style: TextStyle(
                              fontSize: AppSize.sp(30),
                              color: Colours.color_5b5b5b),
                        )
                      ],
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      )
    );
  }
}
